#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"


void QueueInit(Queue* q)
{
	assert(q);
	q->head = NULL;
	q->tail = NULL;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* newNode = (QNode*)malloc(sizeof(QNode));
	newNode->data = data;
	newNode->next = NULL;
	// 第一次插入
	if (q->head == NULL)
	{
		q->head = q->tail = newNode;
	}
	else
	{
		q->tail->next = newNode;
		q->tail = newNode;
	}
}

bool QueueEmpty(Queue* q)
{
	assert(q);
	return q->head == NULL;
}


void QueuePop(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	QNode* next = q->head->next;
	free(q->head);
	q->head = next;
	if (q->head == NULL)
	{
		q->tail = NULL;
	}

}


QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	return q->head->data;
}

QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	return q->tail->data;
}


int QueueSize(Queue* q)
{
	assert(q);
	int count = 0;
	QNode* current = q->head;
	while (current != NULL)
	{
		count++;
		current = current->next;
	}
	return count;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	//assert(!QueueEmpty(q));
	QNode* current = q->head;
	while (current != NULL)
	{
		QNode* next = current->next;
		free(current);
		current = next;
	}
	q->head = q->tail = NULL;
}


//void QueueDestroy(Queue* q)
//{
//	assert(q);
//	assert(!QueueEmpty(q));
//	while (q->head != NULL)
//	{
//		QNode* next = q->head->next;
//		free(q->head);
//		q->head = next;
//		if (next != NULL)
//		{
//			next = next->next;
//		}
//	}
//	q->tail = NULL;
//}
